package com.jxc.action.dls;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.po.DealerGrantRecord;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VAgencyGrantRecord;
import com.jxc.model.vo.VDealerGrantRecord;
import com.jxc.model.vo.VTransferlist;
import com.jxc.model.vo.VUser;
import com.jxc.service.AgencyServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/dls")
@Action(value = "agencyAction", results = {
		@Result(name = "agency", location = "/agency/agency.jsp"),
		@Result(name = "agencyAdd", location = "/agency/agencyAdd.jsp") })
public class AgencyAction extends BaseAction implements
		ModelDriven<VDealerGrantRecord> {

	@Autowired
	private AgencyServiceI agencyService;
	private VDealerGrantRecord model = new VDealerGrantRecord();
	@Autowired
	private UserServiceI userService;
	@Override
	public VDealerGrantRecord getModel() {
		return model;
	}

	public String agency() {
		getSubAuths("WDDLS_WDDLS");
		return "agency";
	}

	public void datagrid() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext
				.getRequest().getSession().getAttribute(
						ResourceUtil.getSessionInfoName());
		model.setDealerid(sessionInfo.getUserId());
		writeJson(agencyService.datagrid(model));
	}

	public String agencyAdd() {
		return "agencyAdd";
	}

	
	/**
	 * 确认与代理商建立关联
	 * 
	 */
	public void grantConfirm() {
		Json j = new Json();
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			Long id = null;
			if(model.getId()!=null){
				id = model.getId();
				agencyService.grantConfirm(id);
				j.setMsg("确认成功!");
				j.setSuccess(true);
			}else{
				j.setMsg("确认失败:未传入正确的dgrId！");
			}
			
		} catch (Exception e) {
			j.setMsg("确认失败：确认时发生异常！");
			e.printStackTrace();
		}
		super.writeJson(j);
	}

	public void add() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext
				.getRequest().getSession().getAttribute(
						ResourceUtil.getSessionInfoName());
		model.setDealerid(sessionInfo.getUserId());
		Integer result = agencyService.save(model);
		Json j = new Json();
		if (result == 1) {
			j.setMsg("添加失败，已经授权过！");
			j.setSuccess(false);
		} else {
			j.setMsg("添加成功！");
			j.setSuccess(true);
		}
		super.writeJson(j);
	}
	
	public void delete(){
		if(model.getIds()!=null&&!model.getIds().equals("")){
			agencyService.delete(model.getIds());
		}
	}
	/**
	 * 查询当前经销商所有已经关联的代理商，给前台一个json
	 */
	public void noAuth_MyAgencyJson() {
		List<VUser> agency = new ArrayList<VUser>();

		try{
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			VUser vuser = userService.get((userService.get(sessionInfo.getUserId())).getUpid());
			agency = userService.findAllMyAgency(vuser);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		this.writeJson(agency);
	}

}
